<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\HttpController\adminapi;

use App\Services\System\Admin\SystemAdminServices;
use App\Utility\VerifyCodeTools;
use EasySwoole\EasySwoole\Config;
use EasySwoole\FastCache\Cache;
use EasySwoole\Http\Message\Status;
use EasySwoole\Utility\Random;
use EasySwoole\Validate\Validate;
use EasySwoole\VerifyCode\Conf;
use EasySwoole\VerifyCode\VerifyCode;

/**
 * Description of Login
 *
 * @author zike
 */
class Login extends AuthController {

    static $VERIFY_CODE_TTL = 120;
    static $VERIFY_CODE_LENGTH = 4;

    protected function getValidateRule(?string $action): ?Validate {
	switch ($action) {
	    case 'login':
		$validate = new Validate();
//                $validate->addColumn('imgcode')->required();
//                $validate->addColumn('account')->required()->alphaDash();
//                $validate->addColumn('pwd')->required();
		return $validate;
		break;
	}
	return null;
    }

    /**
     * 验证码
     * @return $this|\think\Response
     */
    public function captcha() {
	// 配置验证码
	$cfg = new Conf();
	$cfg->setUseNoise();
	$vCode = new VerifyCode($cfg);

	// 获取随机数(即验证码的具体值)
	$random = Random::character(self::$VERIFY_CODE_LENGTH, '1234567890abcdefghijklmnopqrstuvwxyz');
	// 绘制验证码
	$code = $vCode->DrawCode($random);

	// 获取验证码的 base64 编码及设置验证码有效时间
	$time = time();

	// 将验证码加密存储在 Cookie 中，方便进行后续验证。用户也可以把验证码保存在 Session 或者 Redis中，方便后续验证。
	$this->response()->setCookie("verifyCodeHash", VerifyCodeTools::getVerifyCodeHash($random, $time), $time + self::$VERIFY_CODE_TTL, '/');
	$this->response()->setCookie('verifyCodeTime', $time, $time + self::$VERIFY_CODE_TTL, '/');

	$this->response()->withHeader('Content-Type', 'image/png');
	$this->response()->write($code->getImageByte());
    }

    /**
     * 登陆
     * @return mixed
     */
    public function login() {

	$code = $this->request()->getRequestParam('imgcode');
	if (!VerifyCodeTools::checkVerifyCode($code, $this->request()->getCookieParams('verifyCodeTime'), $this->request()->getCookieParams('verifyCodeHash'))) {
	    $this->writeJson(Status::CODE_OK, '验证码错误，请重新输入!', []);
	    return false;
	}

	$account = $this->request()->getRequestParam('account') ?? '';
	$password = $this->request()->getRequestParam('pwd') ?? '';
	$ret = SystemAdminServices::getInstance()->login($account, $password, 'admin', $this->clientRealIP());
	$this->writeJson(Status::CODE_OK, $ret, 'ok');
    }

    /**
     * 获取后台登录页轮播图以及LOGO
     * @return mixed
     */
    public function info() {
	$ret = [
	    'slide' => '', // sys_data('admin_login_slide') ?? [],
	    'logo_square' => stripslashes(Config::getInstance()->getConf('site_logo_square')), // sys_config('site_logo_square'),//透明
	    'logo_rectangle' => Config::getInstance()->getConf('site_logo'), // sys_config('site_logo'),//方形
	    'login_logo' => Config::getInstance()->getConf('login_logo'), // sys_config('login_logo')//登陆
	];

	$this->writeJson(Status::CODE_OK, $ret, 'ok');
    }

    public function login2() {
	echo "login2233";
    }

    public function login3() {
	echo "login2";
    }

}
